package com.wzw.basics.encryption;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * @author Wangzhiwen
 */
public class HmacSHA1Encryption {

    public static String encrypt(String encryptText) {
        String encryptKey = "";
        SecretKeySpec secretKeySpec = new SecretKeySpec(encryptKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
        Mac instance;
        try {
            instance = Mac.getInstance(secretKeySpec.getAlgorithm());
            instance.init(secretKeySpec);
        } catch (Exception e) {
            throw new RuntimeException("HmacSHA1 Encrypt Error", e);
        }
        byte[] doFinal = instance.doFinal(encryptText.getBytes(StandardCharsets.UTF_8));
        StringBuilder builder = new StringBuilder();
        String temp;
        for (byte b : doFinal) {
            temp = Integer.toHexString(b & 0XFF);
            if (temp.length() == 1) {
                builder.append('0');
            }
            builder.append(temp);
        }
        return builder.toString();
    }

}
